package com.service.impl;

import com.entity.EngineerProjectInfo;
import com.repository.EngineerProjectInfoRepository;
import com.service.EngineerProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional(readOnly = true)
public class EngineerProjectInfoServiceImpl implements EngineerProjectInfoService {

    @Autowired
    private EngineerProjectInfoRepository engineerProjectInfoRepository;

    @Override
    @Transactional
    public EngineerProjectInfo save(EngineerProjectInfo engineerProjectInfo) {
        return engineerProjectInfoRepository.save(engineerProjectInfo);
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        engineerProjectInfoRepository.deleteById(id);
    }

    @Override
    @Transactional
    public EngineerProjectInfo update(Integer id, EngineerProjectInfo engineerProjectInfo) {
        Optional<EngineerProjectInfo> existing = engineerProjectInfoRepository.findById(id);
        if (existing.isPresent()) {
            engineerProjectInfo.setId(Math.toIntExact(id));
            return engineerProjectInfoRepository.save(engineerProjectInfo);
        }
        throw new RuntimeException("未找到ID为 " + id + " 的工程项目信息");
    }

    @Override
    public Optional<EngineerProjectInfo> findById(Integer id) {
        return engineerProjectInfoRepository.findById(id);
    }

    @Override
    public List<EngineerProjectInfo> findAll() {
        return engineerProjectInfoRepository.findAll();
    }

    @Override
    public Page<EngineerProjectInfo> findAll(Pageable pageable) {
        return engineerProjectInfoRepository.findAll(pageable);
    }
}